import '../function.dart';
import 'dart:async';

class DeviceModel {
  int id;
  int group_id;
  bool loading = false;
  String device_name;
  double yesterday_income = 0.0;
  double prevday_income = 0.0;
  double balance_income = 0.0;
  double total_income = 0.0;
  String account;
  String password;
  String deviceid;
  String imeiid;
  String sessionid;
  String userid;
  int capacity_1;
  int used_1;
  int capacity_2;
  int used_2;
  String addr;
  String device_id;
  String device_sn;
  String peerid;
  String system_version;
  String ip_addr;
  String mac_address;
  String lan_ip;
  String province;
  String city;
  String isp;
  String status;
  String exception_message;
  String bind_time;
  String bind_days;
  String hardware_model;
  bool is_bind;
  String iswanke;
  String ispush;
  String income_day;
  DeviceModel fromMap(Map device) {
    this.id = device['id'];
    this.device_name = device['device_name'] ?? "";
    this.account = device['account'];
    this.password = device['password'];
    this.deviceid = device['deviceid'];
    this.imeiid = device['imeiid'] ?? "";
    this.sessionid = device['sessionid'];
    this.userid = device['userid'];
    this.capacity_1 = device['capacity_1'] != null ? int.parse(device['capacity_1']) : 0;
    this.used_1 = device['used_1'] != null ? int.parse(device['used_1']) : 0;
    this.capacity_2 = device['capacity_2'] != null ? int.parse(device['capacity_2']) : 0;
    this.used_2 = device['used_2'] != null ? int.parse(device['used_2']) : 0;
    this.addr = device['addr'] ?? "";
    this.device_id = device['device_id'] ?? "";
    this.device_sn = device['device_sn'] ?? "";
    this.peerid = device['peerid'] ?? "";
    this.system_version = device['system_version'] ?? "";
    this.ip_addr = device['ip_addr'] ?? "IP获取中";
    this.mac_address = device['mac_address'] ?? "";
    this.lan_ip = device['lan_ip'] ?? "";
    this.bind_days = device['bind_days'] ?? "";
    this.province = device['province'] ?? "地区";
    this.city = device['city'] ?? "地区";
    this.isp = device['isp'] ?? "运营商";
    this.status = device['status'] ?? "";
    this.bind_time = device['bind_time'] ?? "";
    this.hardware_model = device['hardware_model'] ?? "";
    this.income_day=device['income_day']??"";
    this.ispush=device['ispush']??device['ispush'];
    if (device['is_bind'] == null) {
      this.is_bind = true;
    } else {
      this.is_bind = device['is_bind'] == "1";
    }
    this.iswanke = device['iswanke'] ?? "";
    this.ispush = device['ispush'] ?? "";
    this.group_id = device['group_id'] ?? "";
    if(this.income_day==getDate(-1)){
      this.yesterday_income=device['yesterday_income']??0.0;
      this.prevday_income=device['prevday_income']??0.0;
      this.balance_income=device['balance_income']??0.0;
      this.total_income=device['total_income']??0.0;
    }else if(this.income_day==getDate(-2)){
      this.yesterday_income=0.0;
      this.prevday_income=device['prevday_income']??0.0;
      this.balance_income=0.0;
      this.total_income=0.0;
    }
    return this;
  }

  Future<bool> update() async {
    var res = await updateDevice(
      id: this.id,
      device_name: this.device_name,
      password: this.password,
      sessionid: this.sessionid,
      userid: this.userid,
      capacity_1: this.capacity_1,
      used_1: this.used_1,
      capacity_2: this.capacity_2,
      used_2: this.used_2,
      addr: this.addr,
      device_id: this.device_id,
      device_sn: this.device_sn,
      peerid: this.peerid,
      system_version: this.system_version,
      ip_addr: this.ip_addr,
      lan_ip: this.lan_ip,
      mac_address: this.mac_address,
      province: this.province,
      city: this.city,
      isp: this.isp,
      status: this.status,
      bind_time: this.bind_time,
      bind_days: this.bind_days,
      hardware_model: this.hardware_model,
      is_bind: this.is_bind ? "1" : "0",
      iswanke: this.iswanke,
      group_id: this.group_id,
      ispush: this.ispush,
      yesterday_income: this.yesterday_income,
      prevday_income: this.prevday_income,
      balance_income: this.balance_income,
      total_income: this.total_income,
      income_day:this.income_day,
    );
    if (res) {
      return true;
    } else {
      return false;
    }
  }

  Future<bool> delete() async {
    var res = await deleteDeviceById(this.id);
    return res;
  }
}
